package com.guit.hrms.service.impl;

import com.guit.hrms.dto.RoleDTO;
import com.guit.hrms.mapper.RoleMapper;
import com.guit.hrms.po.Permission;
import com.guit.hrms.po.Role;
import com.guit.hrms.service.RoleService;
import com.guit.hrms.vo.RoleVO;
import com.guit.hrms.vo.api.ApiVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 查询角色和权限
     * @return 角色和权限信息
     */
    @Override
    public RoleVO queryRole() {
        List<Role> roles = roleMapper.queryRoles();
        for (Role role : roles) {
            List<Permission> permissions = roleMapper.queryPermissionByRoleId(role.getId());
            role.setPermissions(permissions);
            List<ApiVO> apis = roleMapper.queryApiByRoleId(role.getId());
            role.setApis(apis);
        }

        List<Permission> permissions = roleMapper.queryPermissions();
        List<ApiVO> apis = roleMapper.queryAllApi();
        return new RoleVO(roles, permissions,apis);
    }

    /**
     * 添加角色
     * @param roleDTO 角色信息
     * @return 添加结果
     */
    @Transactional
    @Override
    public Integer addRole(RoleDTO roleDTO) {
        Role name = roleMapper.getRoleByName(roleDTO.getName());
        if (name != null) {
            return 0;
        }
        Integer r = roleMapper.addRole(roleDTO);
        Integer r1 = 0;
        for (Integer permission : roleDTO.getPermissions()) {
            r1 = roleMapper.addRolePermission(roleDTO.getId(), permission);
            if (r1 == 0) {
                throw new RuntimeException("新增失败");
            }
        }
        for (Integer api : roleDTO.getApis()) {
            r1 = roleMapper.addRoleApi(roleDTO.getId(),api);
            if (r1 == 0) {
                throw new RuntimeException("新增失败");
            }
        }
        if (r*r1 == 0) {
            throw new RuntimeException("新增失败");
        } else {
            return 1;
        }
    }

    /**
     * 更新角色
     * @param roleDTO 角色信息
     * @return 更新结果
     */
    @Transactional
    @Override
    public Integer updateRole(RoleDTO roleDTO) {
        Integer r = 0;
        Integer row = 0;
        if (roleDTO.getPermissions() != null){
            Integer num = roleMapper.getPermissionsByRole(roleDTO.getId());
            if (num>roleDTO.getPermissions().size()){
                List<Integer> id = new ArrayList<>();
                id.add(roleDTO.getId());
                Integer result = roleMapper.deleteRolePermission(id);
                if (result == 0){
                    throw new RuntimeException("新增失败");
                }
            }
            for (Integer permission : roleDTO.getPermissions()) {
                row = roleMapper.getRolePermission(roleDTO.getId(), permission);
                if (row == null) {
                    r = roleMapper.addRolePermission(roleDTO.getId(), permission);
                } else {
                    r = roleMapper.updateRolePermission(row,roleDTO.getId(), permission);
                }
                if (r == 0) {
                    throw new RuntimeException("更新失败");
                }
            }
        }
        if (roleDTO.getApis() != null){
            Integer num = roleMapper.getApisByRole(roleDTO.getId());
            if (num>roleDTO.getApis().size()){
                List<Integer> id = new ArrayList<>();
                id.add(roleDTO.getId());
                Integer result = roleMapper.deleteRoleApi(id);
                if (result == 0){
                    throw new RuntimeException("新增失败");
                }
            }
            for (Integer api : roleDTO.getApis()) {
                row = roleMapper.getRoleApi(roleDTO.getId(), api);
                if (row == null) {
                    r = roleMapper.addRoleApi(roleDTO.getId(), api);
                } else {
                    r = roleMapper.updateRoleApi(row,roleDTO.getId(), api);
                }
                if (r == 0) {
                    throw new RuntimeException("更新失败");
                }
            }
        }
        if (roleDTO.getName() == null && roleDTO.getDescription() == null) {
            return r;
        } else {
            r = roleMapper.updateRole(roleDTO);
        }
        if (r == 0) {
            throw new RuntimeException("更新失败");
        }
        return r;
    }

    /**
     * 删除角色
     * @param roleIds 角色id列表
     * @return 删除结果
     */
    @Transactional
    @Override
    public Integer deleteRole(List<Integer> roleIds) {
        Integer r1 = roleMapper.deleteRole(roleIds);
        Integer r2 = roleMapper.deleteRolePermission(roleIds);
        Integer r3 = roleMapper.deleteRoleApi(roleIds);
        if (r1*r2*r3 == 0 || r1 != roleIds.size()) {
            throw new RuntimeException("删除失败");
        } else {
            return 1;
        }
    }
}
